package com.power.sifa.approve.model.doc;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Apl_applicant;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.Apl_thirdper;
import com.power.sifa.approve.model.enumeration.Tp_procmaterial;
import com.power.sifa.approve.service.Apl_admreviewService;
import com.power.sifa.approve.service.Apl_applicantService;
import com.power.sifa.approve.service.Apl_mainService;
import com.power.sifa.approve.service.Apl_thirdperService;
import lombok.Data;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName ExecuteAuditPerformDOC
 * @Author xiongzw
 * @Date 2023/11/24 10:09
 * @Description 责令履行行政复议决定通知书
 * @Version: 1.0
 **/

@Data
public class ExecuteAuditPerformDoc extends AbstractDoc {

    /**
     * 被申请人
     */
    private String apl_admreview_targetname;
    /**
     * 正文
     */
    private String content;

    /**
     * 抄送 （申请人、第三人）
     */
    private String copyto;


    /**
     * 第三人
     */
    private List<Apl_thirdper> thirdperList;


    private final static String CONTENT_TEMPLATE = "    {apl_admreview_applyname}对你机关[{behavior}]不服提出行政复议申请，本府已作出行政复议决定{prcode}，并已送达你机关，现你机关未依法履行该行政复议决定。根据《中华人民共和国行政复议法》第三十二条的规定，责令你机关于{today}前履行，并将履行结果书面报告本府。";


    public ExecuteAuditPerformDoc() {
        super();
        this.setDocType(Tp_procmaterial.HEAR_PERFORM);
    }

    public ExecuteAuditPerformDoc(String applyid) {
        super(applyid);
        this.setDocType(Tp_procmaterial.HEAR_PERFORM);
    }

    public ExecuteAuditPerformDoc(String applyid, String apl_admreview_targetname, String content, String copyto) {
        super(applyid);
        this.setDocType(Tp_procmaterial.HEAR_PERFORM);
        this.apl_admreview_targetname = apl_admreview_targetname;
        this.content = content;
        this.copyto = copyto;
    }



    @Override
    public void initDocParam (String applyid) {
        Apl_admreviewService apl_admreviewService = SpringUtil.getBean(Apl_admreviewService.class);
        Apl_mainService apl_mainService =  SpringUtil.getBean(Apl_mainService.class);
        Apl_applicantService apl_applicantService =  SpringUtil.getBean(Apl_applicantService.class);
        Apl_thirdperService apl_thirdperService = SpringUtil.getBean(Apl_thirdperService.class);

        Apl_admreview admreview = apl_admreviewService.getById(applyid);
        Apl_main apl_main = apl_mainService.getById(applyid);
        List<Apl_applicant> applicantList = apl_applicantService.getApplicantByApplyid(applyid);
        this.setAdmreObj(admreview);
        this.setApl_admre_audit_code(apl_main.getAcceptcode());
        this.setFaMain(apl_main);
        this.setApplicantList(applicantList);

        List<Apl_thirdper> thirdperList  = apl_thirdperService.getThirdListByApplyid(applyid);
        this.setThirdperList(thirdperList);
    }



    @Override
    public AbstractDoc generateTemplate(String applyid) {
        ExecuteAuditPerformDoc template = new ExecuteAuditPerformDoc(applyid);
        // 案号
        template.setApl_admre_audit_code(this.getApl_admre_audit_code());
        template.setApl_admreview_targetname(ObjectUtil.isNotNull(this.getAdmreObj()) ? this.getAdmreObj().getTargetname() : "");
        String content = CONTENT_TEMPLATE.replace("{apl_admreview_applyname}", ObjectUtil.isNotNull(this.getAdmreObj()) ?
                this.getAdmreObj().getApplyname() : "" );
        content = content.replace("{behavior}", this.transBehavior());
        content = content.replace("{prcode}", this.transPrcode());
        content = content.replace("{today}", this.transModelToday(Boolean.FALSE));
        template.setContent(content);
        StringBuffer copyto = new StringBuffer();
        Set<String> person = new HashSet<>();
        if (ObjectUtil.isNotNull(this.getAdmreObj())) {
            person.addAll(Arrays.asList(this.getAdmreObj().getApplyname().split(",")));
        }
        if (CollectionUtil.isNotEmpty(this.thirdperList)) {
            person.addAll(this.thirdperList.stream().map(Apl_thirdper::getName).collect(Collectors.toList()));
        }
        template.setCopyto(CollectionUtil.isNotEmpty(person) ? String.join("、",person): "");
        return template;
    }
}
